﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using XBMCRemote.DataModel.Entities;
using XBMCRemote.DataModel.Xbmc;

namespace XBMCRemote.DataModel
{
    public class XbmcHelper
    {
        #region Private Properties
        private JsonRpcClient _client;
        private JsonRpc _jsonRpc;
        private Input _input;
        private VideoLibrary _videoLibrary;

        private bool _connected;
        private IList<Movie> _movies;
        #endregion

        #region Public Events
        public EventHandler SetConnected;
        public EventHandler SetMovies;
        #endregion

        #region Constructors
        public XbmcHelper(Configuration configuration)
        {
            _client = new JsonRpcClient(configuration);

            _jsonRpc = new JsonRpc(_client);
            _input = new Input(_client);
            _videoLibrary = new VideoLibrary(_client);

            _client.PingResult += JsonRpcPingResult;
            _client.GetMoviesResult += JsonRpcGetMoviesResult;
        }

        public XbmcHelper()
        {
        } 
        #endregion

        #region Private Event Handlers
        private void JsonRpcPingResult(object sender, EventArgs<object> e)
        {
            if (!string.IsNullOrEmpty(e.Data.ToString()) && e.Data.ToString() == "pong")
            {
                Connected = true;
            }
            else
            {
                Connected = false;
            }
        }

        private void JsonRpcGetMoviesResult(object sender, EventArgs<object> e)
        {
            if (e.Data != "error")
            {
                JObject movieResultObject = (JObject) e.Data;
                var objectProperties = movieResultObject.Properties();

                foreach (var property in objectProperties)
                {
                    if (property.Name == "movies")
                    {
                        List<Movie> tempMovieList = new List<Movie>();
                        JArray movieArray = property.Value.Value<JArray>();
                        foreach (var movie in movieArray)
                        {
                            var tempMovie = Movie.FromJObject((JObject) movie);
                            tempMovieList.Add(tempMovie);
                        }
                        tempMovieList.Sort((movie1, movie2) => movie1.Title.CompareTo(movie2.Title));
                        Movies = tempMovieList;
                    }
                }
            }
        }
        #endregion

        #region Public methods
        public void Connect()
        {
            if (_jsonRpc != null)
            {
                _jsonRpc.Ping();
            }
            else
            {
                Connected = false;
            }
        }
        #endregion

        #region Public Get/Set properties
        public Input Input { get { return _input; } }

        public VideoLibrary VideoLibrary { get { return _videoLibrary; } }

        public bool Connected
        {
            get
            {
                return _connected;
            }
            set
            {
                _connected = value;
                if (SetConnected != null)
                    SetConnected(null, null);
            }
        }

        public IList<Movie> Movies
        {
            get { return _movies; }
            set 
            { 
                _movies = value;
                if (SetMovies != null)
                    SetMovies(null, null);
            }
        }
        #endregion
    }
}
